package com.imooc.nio;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;

/**
 * @Description
 * @Author
 * @Date 2019/9/5 14:16
 **/
public class NioClientHandler implements Runnable{

    private Selector selector;

    public NioClientHandler(Selector selector){
        this.selector=selector;
    }

    @Override
    public void run() {
        for (;;){
            try {
                int readyChannels = selector.select();
                if(readyChannels==0){
                    continue;
                }
                /**
                 * 获取可用channel的集合
                 */
                Set<SelectionKey> selectionKeys = selector.selectedKeys();

                Iterator<SelectionKey> iterator = selectionKeys.iterator();

                while (iterator.hasNext()){
                    SelectionKey selectionKey = iterator.next();
                    /**
                     * 移除Set中的当前 selectionKey  重点
                     *
                     * selector 检测到 SelectionKey 就会放到selector.selectedKeys() 中，所以处理完要移除
                     */
                    iterator.remove();
                    /**
                     * 7.根据就绪状态，处理具体逻辑
                     *
                     */
                    /**
                     * 如果是 接入事件
                     */
                    if(selectionKey.isAcceptable()){
                        readHandler(selectionKey,selector);
                    }
                    /**
                     * 如果是 可读事件
                     */
                    if(selectionKey.isReadable()){
                        readHandler(selectionKey,selector);
                    }
                }
            }catch (Exception e){

            }


        }
    }
    /**
     * 可读事件处理器
     */
    public void readHandler(SelectionKey selectionKey, Selector selector) throws IOException {
        /**
         * 从selectionKey中获取到已经就绪的channel
         */
        SocketChannel socketChannel= (SocketChannel) selectionKey.channel();
        /**
         * 创建buffer
         */
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        /**
         * 循环读取服务器端端请求信息
         */
        String response="";
        while (socketChannel.read(byteBuffer)>0){
            /**
             * 切换为模式
             */
            byteBuffer.flip();

            /**
             * 读取buffer中的内容
             */
            response+=Charset.forName("UTF-8").decode(byteBuffer);
        }
        /**
         * 将channel再次注册到Selecter上，监听他的可读事件
         */
        socketChannel.register(selector,SelectionKey.OP_READ);
        /**
         * 将服务器端相应信息发送的请求信息广播到其他客户端
         */
        if(response.length()>0){
            //广播给其他客户端
            System.out.println(response);
        }
    }
}
